package js.dwim.client.dialogs;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import org.jivesoftware.smack.packet.Message;

import java.awt.BorderLayout;
import java.awt.Color;

import js.dwim.client.Application;
import js.dwim.client.CommunicationException;
import js.dwim.client.IMessageReceiver;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class Console
        extends JFrame
        implements IMessageReceiver
{
    private static final long serialVersionUID = 1L;

    private JPanel            contentPane;
    private JTextField        txtInput;
    private JTextArea         txtOut;
    private JSplitPane        splitPane;

    /**
     * Create the frame.
     */
    public Console()
    {
        setTitle( "Console" );
        setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
        setBounds( 100, 100, 350, 490 );
        contentPane = new JPanel();
        contentPane.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
        setContentPane( contentPane );
        contentPane.setLayout(new BorderLayout(0, 0));

        splitPane = new JSplitPane();
        splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
        splitPane.setBounds(12, 12, 318, 439);
        contentPane.add( splitPane );
        
        txtInput = new JTextField();
        splitPane.setLeftComponent(txtInput);
        txtInput.addKeyListener( new KeyAdapter() {

            public void keyPressed( KeyEvent e )
            {
                if ( e.getKeyCode() == KeyEvent.VK_ENTER
                        && !txtInput.getText().isEmpty() )
                {
                    String msg = txtInput.getText();
                    try
                    {
                        Application.getState().sendRawMessage( msg );
                        txtOut.append( "\n#" + msg );
                        txtInput.setText( "" );
                    }
                    catch ( CommunicationException ex )
                    {
                        txtOut.append( "\n#Failed to send: " + msg );
                    }
                }
            }
        } );
        txtInput.setForeground( Color.GREEN );
        txtInput.setBackground( Color.BLACK );
        txtInput.setColumns( 10 );
        
        JScrollPane scrollPane = new JScrollPane();
        splitPane.setRightComponent(scrollPane);
        scrollPane.setAutoscrolls( true );
        
        txtOut = new JTextArea();
        scrollPane.setViewportView(txtOut);
        txtOut.setWrapStyleWord( true );
        txtOut.setLineWrap( true );
        txtOut.setEditable( false );
        txtOut.setForeground( new Color( 0, 255, 0 ) );
        txtOut.setBackground( Color.BLACK );
        txtOut.setAutoscrolls( true );
        txtInput.requestFocus();
    }

    public void process( Message msg )
    {
        txtOut.append( "\n" + msg.getBody() );
        txtOut.setCaretPosition( txtOut.getDocument().getLength() );
    }
}
